使用java构建一个挡板程序 您所在的位置:网站首页 java 挡板 使用java构建一个挡板程序

使用java构建一个挡板程序

2024-02-18 02:26| 来源: 网络整理| 查看: 265

在使用include方法的时候,如果使用不当,进场会出现对应模板无法找到的问题。有些情况,我们也想在没有指定模板的情况下能有个默认返回。

这个使用扩展include方法是一个很不错的选择。实现beetl扩展需要做以下步骤:

编写对象,实现IncludeTag类在beetl.properties上增加配置  /** * include 某个文件,如果模板存在则引入,不存在则使用默认的。【默认的不存在则抛出异常】 * 父类参数 : 1:模板名称 2: 额外参数 * 本类增加参数 : 3: 默认模板 * 不满足3参数时与原始include一致 * * @author TWH */ public class IncludeWithDefaultTag extends IncludeTag { @Override public void render() { if (this.args.length == 3) { Object useDefault = this.args[2]; this.args = new Object[]{this.args[0], this.args[1]}; try { super.render(); } catch (BeetlException e) { if (useDefault.equals(this.args[0])) { throw e; } else { if (BeetlException.TEMPLATE_LOAD_ERROR.equals(e.detailCode)) { this.args[0] = useDefault; super.render(); } else { throw e; } } } } else { super.render(); } } }

beetl配置:注意,这里是覆盖了原有include方法,值为类全路径。

## 标签类 # 此处覆盖原始的include 增加一个新功能 失败时使用默认模板 参数为3个时,第三个参数为不存在该模板时使用的模板。 TAG.include=cn.com.twh.IncludeWithDefaultTag

 使用方法: 参数1:目标include模板名,第二个参数:默认的模板。【这里可以使用前缀,匹配不同的资源加载器加载模板。】

源码位置:https://gitee.com/crazyAsm/Tailgate



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有